#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ppmcanvas.h"

#ifndef PPMCANVAS_C
#define PPMCANVAS_C
void init(PpmCanvas *ppm, int w, int h, char *fn){

  (*ppm).width = w;
  (*ppm).height = h;
  (*ppm).filename = fn;
}

void save(PpmCanvas *ppm){
  FILE * fp;
  int iX,iY;  
  const int MaxColorComponentValue = 255;
  static unsigned char color[3];

  fp = fopen((*ppm).filename, "wb");
  (void) fprintf(fp, "P6\n%d %d\n255\n", (*ppm).width, (*ppm).height);

  //unsigned char data[(*ppm).height][(*ppm).width];
  for(iY=0; iY<(*ppm).height; ++iY){  
    for(iX=0; iX<(*ppm).width; ++iX){ 

	  color[0] = iX ;  /* red */
	  color[1] = iY ;  /* green */
	  color[2] = (iX * iY) % 255;  /* blue */
	  (void) fwrite(color, 1, 3, fp);
	  //data[iY][iX]=255;
    }
  }

  //fwrite(data, sizeof data,1,fp);
  fclose(fp);
  printf("OK - file %s saved\n",(*ppm).filename);
}


#endif
